package 算法.leetcode.bytedance;

/**
 * @author lchenglong
 * @date 2022/5/7
 */
public class OfferString_IV {

    public static void main(String[] args) {
        OfferString_IV instance = new OfferString_IV();
        String num1 = "123";
        String num2 = "45";
        String multiply = instance.multiply(num1, num2);
        System.out.println(multiply);
        System.out.println(Long.parseLong(num1)*Long.parseLong(num2));
        System.out.println(Long.MAX_VALUE);
    }

    public String multiply(String num1, String num2) {
        // 边界条件
        if ("0".equals(num1)||"0".equals(num2))return "0";
        int n1 = num1.length(),n2 = num2.length();
        int[] res = new int[n1+n2];
        for (int i=n1-1;i>=0;i--){
            int c1 = num1.charAt(i)-'0';
            for (int j = n2-1;j>=0;j--){
                int c2 = num2.charAt(j)-'0';
                int sum = res[i+j+1] + c2 *c1;
                // 低位在后
                res[i+j+1] = sum %10;
                // 高位在前
                res[i+j] += sum/10;
            }
        }
        StringBuilder ans = new StringBuilder();
        for (int i=0;i<res.length;i++) {
            if (i==0&&res[i]==0)continue;
            ans.append(res[i]);
        }
        return ans.toString();
    }
}
